<script setup lang="ts">
import { getCategory } from '@/api/product/attr/index.ts'
import {
  reqSpuImageList,
  reqSpuSaleAttrList,
  saveSku,
} from '@/api/product/spu/index.ts'
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
let emit = defineEmits(['changeScene'])
let attrList = ref<any>([])
let imgList = ref<any>([])
let saleAttr = ref<any>([])
let skuParams = reactive<any>({
  category3Id: '', //三级分类的ID
  spuId: '', //已有的SPU的ID
  tmId: '', //SPU品牌的ID

  skuName: '', //sku名字
  price: '', //sku价格
  weight: '', //sku重量
  skuDesc: '', //sku的描述

  skuAttrValueList: [
    //平台属性的收集
  ],
  skuSaleAttrValueList: [
    //销售属性
  ],
  skuDefaultImg: '', //sku图片地址
})
let table = ref()
const initSku = async (row: any, c1Id: number, c2Id: number) => {
  skuParams.category3Id = row.category3Id
  skuParams.spuId = row.id
  skuParams.tmId = row.tmId
  let res = await getCategory(c1Id, c2Id, row.category3Id)
  let imageList = await reqSpuImageList(row.id)
  let saleAttrList = await reqSpuSaleAttrList(row.id)
  attrList.value = res.data
  imgList.value = imageList.data
  saleAttr.value = saleAttrList.data
}
const save = async () => {
  skuParams.skuAttrValueList = attrList.value.reduce((prev: any, next: any) => {
    if (next.attrIdAndValueId) {
      let [attrId, valueId] = next.attrIdAndValueId.split(':')
      prev.push({ attrId, valueId })
    }
    return prev
  }, [])
  skuParams.skuSaleAttrValueList = attrList.value.reduce(
    (prev: any, next: any) => {
      if (next.saleIdAndValueId) {
        let [attrId, valueId] = next.saleIdAndValueId.split(':')
        prev.push({ attrId, valueId })
      }
      return prev
    },
    [],
  )
  let res: any = await saveSku(skuParams)
  if (res.code == 200) {
    ElMessage({
      type: 'success',
      message: '添加SKU成功',
    })
    emit('changeScene', { flag: 1, params: 'update' })
  } else {
    ElMessage({
      type: 'error',
      message: '添加SKU失败',
    })
  }
}
const cancel = () => {
  emit('changeScene', { flag: 1, params: 'update' })
}
const setDefault = (row: any) => {
  imgList.value.forEach((item: any) => {
    table.value.toggleRowSelection(item, false)
  })
  table.value.toggleRowSelection(row, true)
  skuParams.skuDefaultImg = row.imgUrl
}
defineExpose({
  initSku,
})
</script>

<template>
  <el-form label-width="100px">
    <el-form-item label="SKU名称">
      <el-input placeholder="SKU名称" v-model="skuParams.skuName"></el-input>
    </el-form-item>
    <el-form-item label="价格(元)">
      <el-input placeholder="价格(元)" v-model="skuParams.price"></el-input>
    </el-form-item>
    <el-form-item label="重量(克)">
      <el-input placeholder="重量(克)" v-model="skuParams.weight"></el-input>
    </el-form-item>
    <el-form-item label="SKU描述">
      <el-input
        placeholder="SKU描述"
        type="textarea"
        v-model="skuParams.skuDesc"
      ></el-input>
    </el-form-item>
    <el-form-item label="平台属性">
      <el-form :inline="true">
        <el-form-item
          v-for="item in attrList"
          :key="item.id"
          :label="item.attrName"
        >
          <el-select v-model="item.attrIdAndValueId">
            <el-option
              :value="`${item.id}:${i.id}`"
              v-for="i in item.attrValueList"
              :key="i.id"
              :label="i.valueName"
            >
              {{ i.valueName }}
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="销售属性">
      <el-form :inline="true">
        <el-form-item
          v-for="item in saleAttr"
          :key="item.id"
          :label="item.saleAttrName"
        >
          <el-select v-model="item.saleIdAndValueId">
            <el-option
              :value="`${item.id}:${i.id}`"
              v-for="i in item.spuSaleAttrValueList"
              :key="i.id"
              :label="i.saleAttrValueName"
            >
              {{ i.saleAttrValueName }}
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="图片名称">
      <el-table border :data="imgList" ref="table">
        <el-table-column
          type="selection"
          width="80px"
          align="center"
        ></el-table-column>
        <el-table-column label="图片">
          <template #="{ row }">
            <img
              :src="row.imgUrl"
              alt="图片"
              style="width: 100px; height: 100px"
            />
          </template>
        </el-table-column>
        <el-table-column label="名称" prop="imgName"></el-table-column>
        <el-table-column label="操作">
          <template #="{ row }">
            <el-button type="primary" plain @click="setDefault(row)">
              设置默认
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="default" @click="save">保存</el-button>
      <el-button type="primary" size="default" @click="cancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<style scoped lang="scss"></style>
